home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / chasm.arc / PRIMER.DOC < prev    next >
Text File  |  1983-08-01  |  46KB  |  1,453 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.                       An Assembly Language Primer
  14.  
  15.                        (C) 1983 by David Whitman
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.                     TABLE OF CONTENTS
  71.  
  72.  
  73.       Introduction.......................................2
  74.  
  75.       The Computer As A Bit Pattern Manipulator..........2
  76.  
  77.       Digression: A Notation System for Bit Patterns.....4
  78.  
  79.       Addressing Memory..................................6
  80.  
  81.       The Contents of Memory: Data and Programs..........7
  82.  
  83.       The Dawn of Assembly Language......................8
  84.  
  85.       The 8088...........................................9
  86.  
  87.       Assembly Language Syntax..........................12
  88.  
  89.       The Stack.........................................14
  90.  
  91.       Software Interrupts...............................15
  92.  
  93.       Pseudo-Operations.................................17
  94.  
  95.       Tutorial..........................................18
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                                                                       2
  135.  
  136.       INTRODUCTION
  137.  
  138.       Many people requesting CHASM have indicated that they are
  139.       interested in *learning* assembly language.  They are beginners,
  140.       and have little idea just where to start.  This primer is
  141.       directed to those users.  Experienced users will probably find
  142.       little here that they do not already know.
  143.  
  144.       Being a primer, this text will not teach you everything there is
  145.       to know about assembly language programming.  It's purpose is to
  146.       give you some of the vocabulary and general ideas which will help
  147.       you on your way.
  148.  
  149.       I must make a small caveat: I consider myself a relative beginner
  150.       in assembly language programming.  A big part of the reason for
  151.       writing CHASM was to try and learn this branch of programming
  152.       from the inside out.  I think I've learned quite a bit, but it's
  153.       quite possible that some of the ideas I relate here may have some
  154.       small, or even large, flaws in them.  Nonetheless, I have
  155.       produced a number of working assembly language programs by
  156.       following the ideas presented here.
  157.  
  158.       THE COMPUTER AS A BIT PATTERN MANIPULATOR.
  159.  
  160.       We all have some conception about what a computer does.  On one
  161.       level, it may be thought of as a machine which can execute BASIC
  162.       programs.  Another idea is that the computer is a number
  163.       crunching device.  As I write this primer, I'm using my computer
  164.       as a word processor.
  165.  
  166.       I'd like to introduce a more general concept of just what sort of
  167.       machine a computer is: a bit pattern manipulator.
  168.  
  169.       I'm certain that everyone has been introduced to the idea of a
  170.       *bit*.  (Note: Throughout this primer, a word enclosed in
  171.       *asterisks* is to be read as if it were in italics.)  A bit has
  172.       two states: on and off, typically represented with the symbols
  173.       "1"  and "0".  In this context, DON'T think of 1 and 0 as
  174.       numbers.  They are merely convenient shorthand labels for the
  175.       state of a bit.
  176.  
  177.       The memory of your computer consists of a huge collection of
  178.       bits, each of which could be in either the 1 or 0 (on or off)
  179.       state.
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.                                                                       3
  201.  
  202.       At the heart of your computer is a microprocessor chip, named the
  203.       8088 by Intel, who makes the chip.  What this chip can do is
  204.       manipulate the bits which make up the memory.  The 8088 likes to
  205.       handle bits in chunks, and so we'll introduce special names for
  206.       the two sizes of bit chunks the 8088 is most happy with.  A
  207.       *byte* will refer to a collection of eight bits.  A *word*
  208.       consists of two bytes, or equivalently, sixteen bits.
  209.  
  210.       A collection of bits holds a pattern, determined by the state of
  211.       it's individual bits.  Here are some typical byte long patterns:
  212.  
  213.       10101010         11111111         00001111
  214.  
  215.       If you've had a course in probability, it's quite easy to work
  216.       out that there are 256 possible patterns that a byte could hold.
  217.       similarly, a word can hold 65,536 different patterns.
  218.  
  219.       All right, now for the single most important idea in assembly
  220.       language programming.  Are you sitting down?  These bit patterns
  221.       can be used to represent other sets of things, by mapping each
  222.       pattern onto a member of the other set.  Doesn't sound like much,
  223.       but IBM has made *BILLIONS* off this idea.
  224.  
  225.       For example, by mapping the patterns a word can hold onto the set
  226.       of integers, you can represent either the numbers from 0 to 65535
  227.       or -32768 to 32767, depending on the exact mapping you use.  You
  228.       might recognize these number ranges as the range of possible line
  229.       numbers, and the possible values of an integer variable, in BASIC
  230.       programs.  This explains these somewhat arbitrary seeming limits:
  231.       BASIC uses words of memory to hold line numbers and integer
  232.       variables.
  233.  
  234.       As another example, you could map the patterns a byte can hold
  235.       onto a series of arbitrarily chosen little pictures which might
  236.       be displayed on a video screen.  If you look in appendix G of
  237.       your BASIC manual, you'll notice that there are *exactly* 256
  238.       different characters that can be displayed on your screen.  Your
  239.       computer uses a byte of memory to tell it what character to
  240.       display at each location of the video screen.
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.                                                                       4
  267.  
  268.       Without getting too far ahead of myself, I'll just casually
  269.       mention that there are about 256 fundamental operations that the
  270.       8088 microprocessor chip can carry out.  This suggests another
  271.       mapping which we'll discuss in more detail later.
  272.  
  273.       The point of this discussion is that we can use bit patterns to
  274.       represent anything we want, and by manipulating the patterns in
  275.       different ways, we can produce results which have significance in
  276.       terms of what we're choosing to represent.
  277.  
  278.       DIGRESSION: A NOTATION SYSTEM FOR BIT PATTERNS
  279.  
  280.       Because of their importance, it would be nice to have a
  281.       convenient way to represent the various bit patterns we'll be
  282.       talking about.  We already have one way, by listing the states of
  283.       the individual bits as a series of 1's and 0's.  This system is
  284.       somewhat clumsy, and error prone.  Are the following word
  285.       patterns identical or different?
  286.  
  287.       1111111011111111                         1111111101111111
  288.  
  289.       You probably had trouble telling them apart.  It's easier to tell
  290.       that they're different by breaking them down into more manageable
  291.       pieces, and comparing the pieces.  Here are the same two patterns
  292.       broken down into four bit chunks:
  293.  
  294.       1111 1110 1111 1111                  1111 1111 0111 1111
  295.  
  296.       Some clown has given the name *nybble* to a chunk of 4 bits,
  297.       presumably because 4 bits are half a byte.  A nybble is fairly
  298.       easy to handle.  There are only 16 possible nybble long patterns,
  299.       and most people can distinguish between the patterns quite
  300.       easily.
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.                                                                       5
  333.  
  334.       Each nybble pattern has been given a unique symbol agreed upon by
  335.       computer scientists.  The first 10 patterns were given symbols
  336.       "0" through "9", and when they ran out of digit style symbols,
  337.       they used the letters "A" through "F" for the last six patterns.
  338.       Below is the "nybble pattern code":
  339.  
  340.       0000 = 0    0001 = 1    0010 = 2    0011 = 3
  341.  
  342.       0100 = 4    0101 = 5    0110 = 6    0111 = 7
  343.  
  344.       1000 = 8    1001 = 9    1010 = A    1011 = B
  345.  
  346.       1100 = C    1101 = D    1110 = E    1111 = F
  347.  
  348.       Using the nybble code, we can represent the two similar word
  349.       patterns given above, with the following more manageable
  350.       shorthand versions:
  351.  
  352.                      FEFF       FF7F
  353.  
  354.       Of course, the assignment of the symbols for the various nybble
  355.       patterns was not so arbitrary as I've tried to make it appear.  A
  356.       perceptive reader who has been exposed to binary numbers will
  357.       have noticed an underlying system to the assignments.  If the 1's
  358.       and 0's of the patterns are interpreted as actual *numbers*,
  359.       rather than mere symbols for bit states, the first 10 patterns
  360.       correspond to binary numbers whose decimal representation is the
  361.       symbol assigned to the pattern.  The last six patterns receive
  362.       the symbols "A" through "F", and taken together, the symbols 0
  363.       through F constitute the digits of the *hexadecimal* number
  364.       system.  Thus, the symbols assigned to the different nybble
  365.       patterns were born out of historical prejudice in thinking of the
  366.       computer as strictly a number handling machine.  Although this is
  367.       an important interpretation of these symbols, for the time being
  368.       it's enough to merely think of them as a shorthand way to write
  369.       down bit patterns.
  370.  
  371.       Because some nybble patterns can look just like a number, it's
  372.       often necessary to somehow indicate that we're talking about a
  373.       pattern.  In BASIC, you do this by adding the characters &H to
  374.       the beginning of the pattern: &H1234.  A more common convention
  375.       is to just add the letter H to the end of the pattern: 1234H.  In
  376.       both conventions, the H is referring to hexadecimal.
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.                                                                       6
  399.  
  400.       Eventually you'll want to learn about using the hexadecimal
  401.       number system, since it is an important way to use bit patterns.
  402.       I'm not going to discuss it in this primer, because a number of
  403.       books have much better treatments of this topic than I could
  404.       produce.  Consider this an advanced topic you'll want to fill in
  405.       later.
  406.        
  407.       ADDRESSING MEMORY
  408.  
  409.       As stated before, the 8088 chip inside your computer can
  410.       manipulate the bit patterns which make up the computer's memory.
  411.       Some of the possible manipulations are copying patterns from one
  412.       place to another, turning on or turning off certain bits, or
  413.       interpreting the patterns as numbers and performing arithmetic
  414.       operations on them.  To perform any of these actions, the 8088
  415.       has to know what part of memory is to be worked on.  A specific
  416.       location in memory is identified by it's *address*.
  417.  
  418.       An address is a pointer into memory.  Each address points to the
  419.       beginning of a byte long chunk of memory.  The 8088 has the
  420.       capability to distinguish 1,048,576 different bytes of memory.
  421.  
  422.       By this point, it probably comes as no surprise to hear that
  423.       addresses are represented as patterns of bits.  It takes 20 bits
  424.       to get a total of 1,048,576 different patterns, and thus an
  425.       address may be written down as a series of 5 nybble codes.  For
  426.       example, DOS stores a pattern which encodes information about
  427.       what equipment is installed on your IBM PC in the word which
  428.       begins at location 00410.  Interpreting the address as a hex
  429.       number, the second byte of this word has an address 1 greater
  430.       than 00410, or 00411.
  431.  
  432.       The 8088 isn't very happy handling 20 bits at a time.  The
  433.       biggest chunk that's convenient for it to use is a 16 bit word.
  434.       The 8088 actually calculates 20 bit addresses as the combination
  435.       of two words, a segment word and an offset word.  The combination
  436.       process involves interpreting the two patterns as hexadecimal
  437.       numbers and adding them.  The way that two 16 bit patterns can be
  438.       combined to give one 20 bit pattern is that the two patterns are
  439.       added out of alignment by one nybble:
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.                                                                       7
  465.  
  466.           0040      4 nybble segment
  467.            0010     4 nybble offset
  468.          --------
  469.           00410     5 nybble address
  470.  
  471.       Because of this mechanism for calculating addresses, they will
  472.       often be written down in what may be called segment:offset form.
  473.       Thus, the address in above calculation could be written:
  474.  
  475.       0040:0010
  476.  
  477.       MEMORY CONTENTS: DATA AND PROGRAMS
  478.  
  479.       The contents of memory may be broken down into two broad classes.
  480.       The first is *data*, just raw patterns of bits for the 8088 to
  481.       work on.  The significance of the patterns is determined by what
  482.       the computer is being used for at any given time.
  483.  
  484.       The second class of memory contents are *instructions*.  The 8088
  485.       can look at memory and interpret a pattern it sees there as
  486.       specifying one of the 200 some fundamental operations it knows
  487.       how to do.  This mapping of patterns onto operations is called
  488.       the *machine language* of the 8088.  A machine language *program*
  489.       consists of a series of patterns located in consecutive memory
  490.       locations, whose corresponding operations perform some useful
  491.       process.
  492.  
  493.       Note that there is no way for the 8088 to know whether a given
  494.       pattern is meant to be an instruction, or a piece of data to
  495.       operate on.  It is quite possible for the chip to accidentally
  496.       begin reading what was intended to be data, and interpret it as a
  497.       program.  Some pretty bizarre things can occur when this happens.
  498.       In assembly language programming circles, this is known as
  499.       "crashing the system".
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.                                                                       8
  531.  
  532.       THE DAWN OF ASSEMBLY LANGUAGE
  533.  
  534.       Unless you happen to be an 8088 chip, the patterns which make up
  535.       a machine language program can be pretty incomprehensible.  For
  536.       example, the pattern which tells the 8088 to flip all the bits in
  537.       the byte at address 5555 is:
  538.  
  539.       F6 16 55 55
  540.  
  541.       which is not very informative, although you can see the 5555
  542.       address in there.  In ancient history, the old wood-burning and
  543.       vacuum tube computers were programmed by laboriously figuring out
  544.       bit patterns which represented the series of instructions
  545.       desired.  Needless to say, this technique was incredibly tedious,
  546.       and very prone to making errors.  It finally occurred to these
  547.       ancestral programmers that they could give the task of figuring
  548.       out the proper patterns to the computer itself, and assembly
  549.       language programming was born.
  550.  
  551.       Assembly language represents each of the many operations that the
  552.       computer can do with a *mnemonic*, a short, easy to remember
  553.       series of letters.  For example, in boolean algebra, the logical
  554.       operation which inverts the state of a bit is called "not", and
  555.       hence the assembly language equivalent of the preceding machine
  556.       language pattern is:
  557.  
  558.           NOTB [5555]
  559.  
  560.       The brackets around the 5555 roughly mean "the memory location
  561.       addressed by".  The "B" at the end of "NOTB" indicates that we
  562.       want to operate on a byte of memory, not a word.
  563.  
  564.       Unfortunately, the 8088 can't make head nor tail of the string of
  565.       characters "NOTB".  What's needed is a special program to run on
  566.       the 8088 which converts the string "NOTB" into the pattern F6 16.
  567.       This program is called an assembler.  A good analogy is that an
  568.       assembler program is like a meat grinder which takes in assembly
  569.       language and gives out machine language.
  570.  
  571.       Typically, an assembler reads a file of assembly language and
  572.       translates it one line at a time, outputting a file of machine
  573.       language.  Often times the input file is called the *source file*
  574.       and the output file is called the *object file*.  The machine
  575.       language patterns produced are called the *object code*.
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.                                                                       9
  597.  
  598.       Also produced during the assembly process is a *listing*, which
  599.       summarizes the results of the assembly process.  The listing
  600.       shows each line from the source file, along with the shorthand
  601.       "nybble code" representation of the object code produced.  In the
  602.       event that the assembler was unable to understand any of the
  603.       source lines, it inserts error messages in the listing, pointing
  604.       out the problem.
  605.  
  606.       The primeval assembly language programmers had to write their
  607.       assembler programs in machine language, because they had no other
  608.       choice.  Not being a masochist, I wrote CHASM in BASIC.  When you
  609.       think about it, there's a sort of circular logic in action here.
  610.       Some programmers at Microsoft wrote the BASIC interpreter in
  611.       assembly language, and I used BASIC to write an assembler.
  612.       Someday, I hope to use the present version of CHASM to produce a
  613.       machine language version, which will run about a hundred times
  614.       faster, and at the same time bring this crazy process full
  615.       circle.
  616.        
  617.       THE 8088
  618.  
  619.       The preceding discussions have (I hope) given you some very
  620.       general background, a world view if you will, about assembly and
  621.       machine language programming.  At this point, I'd like to get
  622.       into a little more detail, beginning by examining the internal
  623.       structure of the 8088 microprocessor, from the programmer's point
  624.       of view.  This discussion is a condensation of information which
  625.       I obtained from "The 8086 Book" which was written by Russell
  626.       Rector and George Alexy, and published by Osborne/McGraw-Hill.
  627.       Once you've digested this, I'd recommend going to The 8086 Book
  628.       for a deeper treatment.  To use the CHASM assembler, you're going
  629.       to need The 8086 Book anyway, to tell you the different 8088
  630.       instructions and their mnemonics.
  631.  
  632.       Inside the 8088 are a number of *registers* each of which can
  633.       hold a 16 bit pattern.  In assembly language, each of the
  634.       registers has a two letter mnemonic name.  There are 14
  635.       registers, and their mnemonics are:
  636.  
  637.       AX BX CX DX     SP BP    SI DI     CS DS SS ES    PC ST
  638.  
  639.       Each of the registers are a little different and have different
  640.       intended uses, but they can be grouped into some broad classes.
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.                                                                      10
  663.  
  664.       The *general purpose* registers (AX BX CX DX) are just that.
  665.       These are registers which hold patterns pulled in from memory
  666.       which are to be worked on within the 8088.  You can use these
  667.       registers for just about anything you want.
  668.  
  669.       Each of the general purpose registers can be broken down into two
  670.       8 bit registers, which have names of their own.  Thus, the CX
  671.       register is broken down into the CH and CL registers.  The "H"
  672.       and "L" stand for high and low respectively.  Each general
  673.       purpose register breaks down into a high/low pair.
  674.  
  675.       The AX register, and it's 8 bit low half, the AL register, are
  676.       somewhat special.  Mainly for historical reasons, these registers
  677.       are referred to as the 16 bit and 8 bit *accumulators*.  Some
  678.       operations of the 8088 can only be carried out on the contents of
  679.       the accumulators, and many others are faster when used in
  680.       conjunction with these registers.
  681.  
  682.       Another group of registers are the *segment* registers (CS DS SS
  683.       ES).  These registers hold segment values for use in calculating
  684.       memory addresses.  The CS, or code segment register, is used
  685.       every  time the 8088 accesses memory to read an instruction
  686.       pattern.  The  DS, or data segment register, is used for bringing
  687.       data patterns in.  The SS register is used to access the stack
  688.       (more about the stack later).  The ES is the extra segment
  689.       register.  A very few special instructions use the ES register to
  690.       access memory, plus you can override use of the DS register and
  691.       substitute the ES register, if you need to maintain two separate
  692.       data areas.
  693.  
  694.       The *pointer* (SP BP) and *index* (DI SI) registers are used to
  695.       provide indirect addressing, which is an very powerful technique
  696.       for accessing memory.  Indirect addressing is beyond the scope of
  697.       this little primer, but is discussed in The 8086 Book.  The SP
  698.       register is used to implement a stack in memory. (again, more
  699.       about the stack later)  Besides their special function, the BP,
  700.       DI and SI registers can be used as additional general purpose
  701.       registers.  Although it's physically possible to directly
  702.       manipulate the value in the SP register, it's best to leave it
  703.       alone, since you could wipe out the stack.
  704.  
  705.       Finally, there are two registers which are relatively
  706.       inaccessible to direct manipulation.  The first is the *program
  707.       counter*, PC.  This register always contains the offset part of
  708.       the address of the next instruction to be executed.  Although
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.                                                                      11
  729.  
  730.       you're not allowed to just move values into this register, you
  731.       *can* indirectly affect it's contents, and hence the next
  732.       instruction to be executed, using operations which are equivalent
  733.       to BASIC's GOTO and GOSUB instructions.  Occasionally, you will
  734.       see the PC referred to as the *IP*, which stands for instruction
  735.       pointer.
  736.  
  737.       The last register is also relatively inaccessible.  This is the
  738.       *status* register, ST.  This one has a *two* alternate names, so
  739.       watch for FL (flag register) and PSW (program status word).  The
  740.       latter is somewhat steeped in history, since this was the name
  741.       given to a special location in memory which served a similar
  742.       function on the antique IBM 360 mainframe.
  743.  
  744.       The status register consists of a series of one bit *flags* which
  745.       can affect how the 8088 works.  There are special instructions
  746.       which allow you to set or clear each of these flags.  In
  747.       addition, many instructions affect the state of the flags,
  748.       depending on the outcome of the instruction.  For example, one of
  749.       the bits of the status register is called the Zero flag.  Any
  750.       operation which ends up generating a bit pattern of all 0's
  751.       automatically sets the Zero flag on.
  752.  
  753.       Setting the flags doesn't seem to do much, until you know that
  754.       there a whole set of conditional branching instructions which
  755.       cause the equivalent to a BASIC GOTO if the particular flag
  756.       pattern they look for is set.  In assembly language, the only way
  757.       to make a decision and branch accordingly is via this flag
  758.       testing mechanism.
  759.  
  760.       Although some instructions implicitly affect the flags, there are
  761.       a series of instructions whose *only* effect is to set the flags,
  762.       based on some test or comparison.  It's very common to see one
  763.       of these comparison operations used to set the flags just before
  764.       a conditional branch.  Taken together, the two instructions are
  765.       exactly equivalent to BASIC's:
  766.  
  767.       IF (comparison) THEN GOTO (linenumber)
  768.  
  769.  
  770.  
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.  
  778.  
  779.  
  780.  
  781.  
  782.  
  783.  
  784.  
  785.  
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.  
  794.                                                                      12
  795.  
  796.       ASSEMBLY LANGUAGE SYNTAX
  797.  
  798.       In general, each line of an assembly language program translates
  799.       to a set of patterns which specify one fundamental operation for
  800.       the 8088 to carry out.
  801.  
  802.       Each line may consist of one or more of the following parts:
  803.  
  804.       First, a label, which is just a marker for the assembler to use.
  805.       If you want to branch to an instruction from some other part of
  806.       the program, you put a label on the instruction.  When you want
  807.       to branch, you refer to the label.  In general, the label can be
  808.       any string of characters you want.  A good practice is to use a
  809.       name which reminds you what that particular part of the program
  810.       does. CHASM will assume that any string of characters which
  811.       starts in the first column of a line is intended to be a label.
  812.  
  813.       After the label, or if the text of the line starts to the right
  814.       of the first column, at the beginning of the text, comes an
  815.       instruction mnemonic.  This specifies the operation that the line
  816.       is asking for.  For a list of the 200-odd mnemonics, along with
  817.       the instructions they stand for, see The 8086 Book.
  818.  
  819.       Most of the 8088 instructions require that you specify one or
  820.       more *operands*.  The operands are what the operation is to work
  821.       on, and are listed after the instruction mnemonic.
  822.  
  823.       There are a number of possible operands.  Probably the most
  824.       common are registers, specified by their two letter mnemonics.
  825.  
  826.       Another operand type is *immediate data*, a pattern of bits to be
  827.       put somewhere or compared or combined with some other pattern.
  828.       Generally immediate data is specified by it's nybble code
  829.       representation, marked as such by following it with the letter
  830.       "H".  Some assemblers allow alternate ways to specify immediate
  831.       data which emphasize the pattern's intended use.  CHASM
  832.       recognizes five different ways to represent immediate data.
  833.  
  834.       A memory location can be used as an operand.  We've seen one way
  835.       to do this, by enclosing it's address in brackets.  (You can now
  836.       see why the brackets are needed.  Without them, you couldn't
  837.       distinguish between an address and immediate data.)  If you've
  838.       asked the assembler to set aside a section of memory for data
  839.       (more on this latter), and put a label on the request, you can
  840.       specify that point in memory by using the label.  Finally, there
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.                                                                      13
  861.  
  862.       are a number of indirect ways to address memory locations, which
  863.       you can read about in The 8086 Book.
  864.  
  865.       The last major type of operands are labels.  Branching
  866.       instructions require an operand to tell them where to branch
  867.       *to*.  In assembly language, you specify locations which may be
  868.       branched to by putting a label on them.  You can then use the
  869.       label as an operand on branches.
  870.  
  871.       Often times, the order in which the operands are listed can be
  872.       important.  For example, when moving a pattern from one place to
  873.       another, you need to specify where the pattern is to come from,
  874.       and where it's going.  The convention in general use is that the
  875.       first operand is the *destination* and the second is the
  876.       *source*.  Thus, to move the pattern in the DX register into the
  877.       AX register, you would write:
  878.  
  879.               MOV AX,DX
  880.  
  881.       This may take some getting used to, since when reading from left
  882.       to right it seems reasonable to assume that the transfer goes in
  883.       this direction as well.  However, since this convention is pretty
  884.       well entrenched in the assembly language community, CHASM goes
  885.       along with it.
  886.  
  887.       The last part of an assembly language line is a *comment*.  The
  888.       comment is totally ignored by the assembler, but is *vital* for
  889.       humans who are attempting to understand the program.  Assembly
  890.       language programs tend to be very hard to follow, and so it's
  891.       particularly important to put in lots of comments so that you'll
  892.       remember just what it was you were trying to do with a given
  893.       piece of code.  Professional assembly language programmers put a
  894.       comment on *every* line of code, explaining what it does, plus
  895.       devoting many entire lines for additional explanations.  For an
  896.       example, you should examine the BIOS source listing given in the
  897.       IBM Technical Reference manual.  Over *half* the text consists of
  898.       comments!
  899.  
  900.       Since the assembler ignores the comments, they cost you nothing
  901.       in terms of size or speed of execution in the resulting machine
  902.       language program.  This is in sharp contrast to BASIC, where each
  903.       remark slows your program down and eats up precious memory.
  904.  
  905.  
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.  
  913.  
  914.  
  915.  
  916.  
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.  
  926.                                                                      14
  927.  
  928.       Generally, a character is set aside to indicate to the assembler
  929.       the beginning of a comment, so that it knows to skip over.  CHASM
  930.       follows a common convention of reserving the semi-colon (;) for
  931.       marking comments.
  932.        
  933.       THE STACK
  934.  
  935.       I've been dropping the name *stack* from time to time.  The stack
  936.       is just a portion of memory which has been temporarily set aside
  937.       to be used in a special way.
  938.  
  939.       To get a picture of how the stack works, think of the spring
  940.       loaded contraptions you sometimes see holding trays in a
  941.       cafeteria.  As each tray is washed, the busboy puts it on top of
  942.       the stack in the contraption.  Because the thing is spring
  943.       loaded, the whole stack sinks down from the weight of the new
  944.       tray, and the top of the stack ends up always being the same
  945.       height off the floor.  When a customer takes a tray off the
  946.       stack, the next one rises up to take it's place.
  947.  
  948.       In the computer, the stack is used to hold data patterns, which
  949.       are generally being passed from one program or subroutine to
  950.       another.  By putting things on the stack, the receiving routine
  951.       doesn't need to know a particular address to look for the
  952.       information it needs, it just pulls them off the top of the
  953.       stack.
  954.  
  955.       There is some jargon associated with use of the stack.  Patterns
  956.       are *pushed* onto the stack, and *popped* off.  Accordingly,
  957.       there are a set of PUSH and POP instructions in the 8088's
  958.       repertoire.
  959.  
  960.       Because you don't need to keep track of where the patterns are
  961.       actually being kept, the stack is often used as a scratch pad
  962.       area, patterns being pushed when the register they're in is
  963.       needed for some other purpose, then popped out when the register
  964.       is free.  It's very common for the first few instructions of a
  965.       subroutine to be a series of pushes to save the patterns which
  966.       are occupying the registers its about to use.   This is referred
  967.       to as *saving the state* of the registers.  The last thing the
  968.       subroutine will do is pop the patterns back into the registers
  969.       they came from, thus *restoring the state* of the registers.
  970.  
  971.  
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.                                                                      15
  993.  
  994.       Following the analogy of the cafeteria contraption, when you pop
  995.       the stack, the pattern you get is the last one which was pushed.
  996.       When you pop a pattern off, the next-to-last thing pushed
  997.       automatically moves to the top, just as the trays rise up when a
  998.       customer removes one.  Everything comes off the stack in the
  999.       reverse order of which they went on.  Sometimes you'll see the
  1000.       phrase "last in, first out" or *LIFO stack*.
  1001.  
  1002.       Of course, there are no special spring loaded memory locations
  1003.       inside the computer.  The stack is implemented using a register
  1004.       which keeps track of where the top of the stack is currently
  1005.       located.  When you push something, the pointer is moved to the
  1006.       next available memory location, and the pattern is put in that
  1007.       spot.  When something is popped, it is copied from the location
  1008.       pointed at, then the pointer is moved back.  You don't have to
  1009.       worry about moving the pointer because it's all done
  1010.       automatically with the push and pop instructions.
  1011.  
  1012.       The register set aside to hold the pointer is SP, and that's why
  1013.       you don't want to monkey with SP.  You'll recall that to form an
  1014.       address, two words are needed, an offset and a segment.  The
  1015.       segment word for the stack is kept in the SS register, so you
  1016.       should leave SS alone as well.  When you run the type of machine
  1017.       language program that CHASM produces, DOS will automatically set
  1018.       the SP and SS registers to reserve a stack capable of holding 128
  1019.       words.
  1020.        
  1021.       SOFTWARE INTERRUPTS
  1022.  
  1023.       I have been religiously avoiding talking about the various
  1024.       individual instructions the 8088 can carry out, because if I
  1025.       didn't, this little primer would soon grow into a rather long
  1026.       book.  However, there's one very important instruction, which
  1027.       when you read about it in The 8088 Book, won't seem particularly
  1028.       useful.  This section will discuss the *software interrupt*
  1029.       instruction, and why it's so important.
  1030.  
  1031.       The 8088 reserves the first 1024 bytes of memory for a series of
  1032.       256 *interrupt vectors*.  Each of these two word long interrupt
  1033.       vectors is used to store the segment:offset address of a location
  1034.       in memory.  When you execute a software interrupt instruction,
  1035.       the the 8088 pushes the location of the next instruction of your
  1036.       program onto the stack, then branches to the memory location
  1037.       pointed at by the vector specified in the interrupt.
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.  
  1058.                                                                      16
  1059.  
  1060.       This probably seems like a rather awkward way to branch around in
  1061.       memory, and chances are you'd never use this method to get from
  1062.       one part of your program to another.  The way these instructions
  1063.       become important is that IBM has pre-loaded a whole series of
  1064.       useful little (and not so little) machine language routines into
  1065.       your computer, and set the interrupt vectors to point to them.
  1066.       All of these routines are set up so that after doing their thing,
  1067.       they use the location pushed on the stack by the interrupt
  1068.       instruction to branch back to your program.
  1069.  
  1070.       Some of these routines are a part of DOS, and documentation for
  1071.       them can be found in Appendix D of the DOS manual.  The rest of
  1072.       them are stored in ROM (read only memory) and comprise the
  1073.       *BIOS*, or basic input/output system of the computer.  Details of
  1074.       the BIOS routines can be found in Appendix A of IBM's Technical
  1075.       Reference Manual.  IBM charges around $40 for Technical
  1076.       Reference, but the information in Appendix A alone is easily
  1077.       worth the money.
  1078.  
  1079.       The routines do all kinds of useful things, such as run the disk
  1080.       drive for you, print characters on the screen, or read data from
  1081.       the keyboard.  In effect, the software interrupts add a whole
  1082.       series of very powerful operations to the 8088 instruction set.
  1083.  
  1084.       A final point is that if you don't like the way that DOS or the
  1085.       BIOS does something, the vectored interrupt system makes it very
  1086.       easy to substitute your own program to handle that function.  You
  1087.       just load your program and reset the appropriate interrupt vector
  1088.       to point at your program rather than the resident routine.  This
  1089.       is how all those RAM disk and print spooler programs work.  The
  1090.       programs change the vector for disk drive or printer support to
  1091.       point to themselves, and carry out the operations in their own
  1092.       special way.
  1093.  
  1094.       To make things easy for you, one of the DOS interrupt routines
  1095.       has the function of resetting interrupt vectors to point at new
  1096.       code.  Still another DOS interrupt routine is used to graft new
  1097.       code onto DOS, so that it doesn't accidentally get wiped out by
  1098.       other programs.  The whole thing is really quite elegant and easy
  1099.       to use, and IBM is to be complimented for setting things up this
  1100.       way.
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.  
  1124.                                                                      17
  1125.  
  1126.       PSEUDO-OPERATIONS
  1127.  
  1128.       Up to this point, I've implied that each line of an assembly
  1129.       language program gets translated into a machine language
  1130.       instruction.  In fact, this is not the case.  Most assemblers
  1131.       recognize a series of *pseudo-operations* which are handled as
  1132.       embedded commands to the assembler itself, not as an instruction
  1133.       in the machine language program being built.  Almost invariably
  1134.       you'll see the phrase "pseudo-operation" abbreviated down to
  1135.       *pseudo-op*. Sometimes you'll see *assembler directive*, which
  1136.       means the same thing, but just doesn't seem to roll off the
  1137.       tongue as well as pseudo-op.
  1138.  
  1139.       One very common pseudo-op is the *equate*, usually given mnemonic
  1140.       *EQU*.  What this allows you to do is assign a name to a
  1141.       frequently used constant.  Thereafter, anywhere you use that
  1142.       name, the assembler automatically substitutes the equated
  1143.       constant.  This process makes your program easier to read, since
  1144.       in place of the somewhat meaningless looking pattern, you see a
  1145.       name which tells you what the pattern is for.  It also makes your
  1146.       program easier to modify, since if you decide to change the
  1147.       constant, you only need to do it once, rather than all over the
  1148.       program.
  1149.  
  1150.       The only other type of pseudo-op I'll talk about here are those
  1151.       for setting aside memory locations for data.  These pseudo-ops
  1152.       tend to be quite idiosyncratic with each assembler.  CHASM
  1153.       implements two such pseudo-ops: DB (declare byte) and DS (declare
  1154.       storage).  DB is used to set aside small data areas, which can be
  1155.       initialized to any pattern, one byte at a time.  DS sets up
  1156.       relatively large areas, but all the locations are filled with the
  1157.       same initial pattern.
  1158.  
  1159.       If you put a label on a pseudo-op which sets aside data areas,
  1160.       most assemblers allow you to use the label as an operand, in
  1161.       place of the actual address of the location.  The assembler
  1162.       automatically substitutes the address for the name during the
  1163.       translation process.
  1164.  
  1165.       Some assemblers have a great number of pseudo-ops.  CHASM
  1166.       implements a couple more, which aren't discussed here.
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.  
  1190.                                                                      18
  1191.  
  1192.       TUTORIAL
  1193.  
  1194.       To conclude this primer, this section will walk through the
  1195.       process of writing, assembling, and running a very simple
  1196.       program.
  1197.  
  1198.       The program will perform the function filled by the BASIC command
  1199.       CLS, that is, it will clear the video screen and move the cursor
  1200.       to the upper left hand corner.  In fact, this is a useful little
  1201.       program, since the DOS environment doesn't provide any method of
  1202.       clearing the screen.
  1203.  
  1204.       There is a BIOS routine called VIDEO_IO which provides an
  1205.       interface to the screen.  Access to VIDEO_IO is through software
  1206.       interrupt number 16, and documentation can be found on pages A-43
  1207.       and A-44 of Technical Reference.  VIDEO_IO actually performs 15
  1208.       different screen handling functions.  We specify which function
  1209.       we want, along with information needed by the individual
  1210.       function, in the 8088 registers.  Our entire program will be made
  1211.       up of putting the proper patterns into the registers, then
  1212.       activating VIDEO_IO with an interrupt.
  1213.  
  1214.       To clear the screen, we'll use VIDEO_IO's scroll up function.
  1215.       What this does is move a portion of the screen up, filling the
  1216.       vacated space with blanks.  We have to tell VIDEO_IO what portion
  1217.       of the screen to scroll, and how far to scroll it.  We can get
  1218.       the proper patterns into the right registers using the MOV
  1219.       instruction, MOVing the patterns in as immediate data.  Here's
  1220.       the code to do this:
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249.  
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.  
  1256.                                                                      19
  1257.  
  1258.           MOV AH,6     ;this specifies we want a scroll
  1259.                        ;the CH/CL register pair specifies the row and
  1260.                        ;column of the upper left hand corner of the
  1261.                        ;region
  1262.                        ;to be scrolled
  1263.           MOV CH,0     ;row = 0
  1264.           MOV CL,0     ;column = 0
  1265.                        ;the DH/DL pair does the same for the lower
  1266.                        ;right corner.
  1267.           MOV DH,24    ;row = 24
  1268.           MOV DL,79    ;column = 79
  1269.                        ;BH specifies what color to fill with
  1270.           MOV BH,7     ;we'll use black
  1271.                        ;AL specifies how far to scroll.
  1272.           MOV AL,0     ;pattern 0 means to blank out the whole region.
  1273.           INT 16       ;call video_io
  1274.  
  1275.       Notice that none of the lines starts at the left margin (column
  1276.       1).  If they did, CHASM would think that the instruction mnemonic
  1277.       was meant to be a label, and would get very confused.
  1278.  
  1279.       Since the bit patterns are meant to represent numbers, I've
  1280.       chosen to write down the immediate data as decimal numbers.
  1281.       CHASM will automatically translate into the proper patterns.
  1282.       Notice that since each of the high/low register pairs can be
  1283.       accessed as a single 16 bit register, I could have moved the
  1284.       patterns for both halves in at the same time.  I did it this way
  1285.       for clarity. Note also the profusion of comments.
  1286.  
  1287.       The second half of the program has to move the cursor to the
  1288.       upper left.  Again, all that's necessary is to load the registers
  1289.       and execute the interrupt:
  1290.  
  1291.           MOV AH,2     ;specifies that we want to position the cursor.
  1292.                        ;the DH/DL pair specifies the row and column of
  1293.                        ;where we want the cursor.
  1294.           MOV DH,0     ;row = 0
  1295.           MOV DL,0     ;column = 0
  1296.                        ;BH specifies which display page
  1297.           MOV BH,0     ;put the cursor on page 0
  1298.           INT 16       ;call video_io
  1299.  
  1300.  
  1301.  
  1302.  
  1303.  
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.  
  1322.                                                                      20
  1323.  
  1324.       There's one last detail.  We have to warn the 8088 that it's come
  1325.       to the end of our program, or it'll just keep executing whatever
  1326.       random patterns are in memory after our stuff.  Remember
  1327.       "crashing the system"?  One of DOS's vectored interrupts handles
  1328.       program termination, returning you to DOS.  The last instruction
  1329.       is:
  1330.  
  1331.          INT 32       ;return to DOS
  1332.  
  1333.       After writing the program, we must now create a text file which
  1334.       contains the lines of our program.  This is done using a text
  1335.       editor, such as EDLIN, which comes on your DOS disk.  At this
  1336.       point, you can either copy the above lines into a file using an
  1337.       editor, or use the file CLEAR.ASM, which was included on your
  1338.       CHASM disk.  CLEAR.ASM contains the above lines already entered
  1339.       for you, if you'd rather not bother making your own file at the
  1340.       moment.
  1341.  
  1342.       It's now time to assemble the program.  From DOS, you start CHASM
  1343.       up by typing it's name:
  1344.  
  1345.          A> CHASM
  1346.  
  1347.       CHASM will respond by printing a hello screen, and ask you to
  1348.       press a key when you're done reading it.  When you do so, CHASM
  1349.       will ask you some questions:
  1350.  
  1351.           Source code file name? [.asm]
  1352.  
  1353.       Type in the name of the file which has your assembly language
  1354.       program text in it, then press return.
  1355.  
  1356.           Direct listing to Printer (P), Screen (S), or Disk (D)?
  1357.  
  1358.       CHASM wants to know where to send the listing produced during the
  1359.       assembly process.  If you have a printer, turn it on, and then
  1360.       press P.  If you don't have a printer, press S.
  1361.  
  1362.       The last question is:
  1363.  
  1364.           Name for object file? [xxx.com]
  1365.  
  1366.       CHASM is asking for the name you'd like to give to the machine
  1367.       language program which is about to be produced.  Just press enter
  1368.       here. (We'll accept CHASM's default name)
  1369.  
  1370.  
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.  
  1379.  
  1380.  
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.  
  1388.                                                                      21
  1389.  
  1390.  
  1391.       At this point CHASM will start accessing the disk drive, reading
  1392.       in your program a line at a time.  A status line will appear at
  1393.       the bottom of your screen, telling you how far along the
  1394.       translation has gotten.  For this program, the whole process
  1395.       takes about 2 1/2 minutes.
  1396.  
  1397.       If the listing went to your printer, CHASM automatically returns
  1398.       you to DOS when it's finished.  If it went to the screen, CHASM
  1399.       waits for you to press a key to indicate that you're done
  1400.       reading.  Near the bottom of the listing will be the message:
  1401.  
  1402.       XXX Diagnostics Offered
  1403.       YYY Errors Detected
  1404.  
  1405.       If both numbers are 0, everything went fine.  If not, look up on
  1406.       the listing for error messages, which will point out the
  1407.       offending lines.  At this point, don't worry too much about what
  1408.       the error messages say, just fix the line in your input file to
  1409.       look like the text developed above.  Once you manage to get an
  1410.       assembly with no errors, you're ready to go on.
  1411.  
  1412.       Your disk will now contain machine language program whose name is
  1413.       that of your input file, with an extension of .COM.  Check this
  1414.       by typing DIR to get a directory listing.  Not only will this
  1415.       confirm that the file is really there, it fills up your screen,
  1416.       to give us something to clear.
  1417.  
  1418.       To run the machine language program, you just type it's name,
  1419.       with or without the .COM extension.  (Note: even though you don't
  1420.       need to *enter* the it, the file has to have the .COM extension
  1421.       for DOS to recognize it as a machine language program.) If
  1422.       everything was done right, the screen will clear, and then the
  1423.       DOS prompt, A>, will appear.
  1424.  
  1425.       That's the entire process, from start to finish.  At this point
  1426.       you should have enough of a start to be able to digest CHASM's
  1427.       documentation and The 8086 Book, then begin to write your own
  1428.       programs.  Good Luck!
  1429.  
  1430.  
  1431.  
  1432.  
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438.  
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444.  
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450.  
  1451.  
  1452.  
  1453.